﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using DevExpress.XtraGrid.Views.Grid;

namespace KQReport.Utils
{
    public class ExportHelper
    {

        public static void ExportToEx(GridView gridView, String filename, string ext,IWin32Window windows)
        {
            var dic = System.IO.Path.GetDirectoryName(filename);
            if (!System.IO.Directory.Exists(dic))
                System.IO.Directory.CreateDirectory(dic);
            var currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            //            DevExpress.XtraPrinting.IPrintingSystem ps = DevExpress.XtraPrinting.PrintHelper.GetCurrentPS();
            //            ps.AfterChange += new DevExpress.XtraPrinting.ChangeEventHandler(Export_ProgressEx);
            if (ext == "*.rtf") gridView.ExportToRtf(filename);
            if (ext == "*.pdf")
            {
                var fontcurrent = gridView.Appearance.Row.Font;
                gridView.Appearance.Row.Font = new Font("宋体", fontcurrent.Size, fontcurrent.Style);
                gridView.ExportToPdf(filename);
                gridView.Appearance.Row.Font = fontcurrent;
            }
            if (ext == "*.mht") gridView.ExportToMht(filename);
            if (ext == "*.htm") gridView.ExportToHtml(filename);
            if (ext == "*.txt") gridView.ExportToText(filename);
            if (ext == "*.xls") gridView.ExportToXls(filename);
            if (ext == "*.xlsx") gridView.ExportToXlsx(filename);
            //            ps.AfterChange -= new DevExpress.XtraPrinting.ChangeEventHandler(Export_ProgressEx);
            Cursor.Current = currentCursor;
            if (windows != null)
                OpenFile(windows, filename);
        }

        public static void OpenFile(IWin32Window window, string fileName)
        {
            if (!Dialog.MsgAsk(window, @"打开此文件")) return;
            try
            {
                var process = new Process
                {
                    StartInfo =
                    {
                        FileName = fileName,
                        Verb = "Open",
                        WindowStyle = ProcessWindowStyle.Normal
                    }
                };
                process.Start();
            }
            catch
            {
                Dialog.MsgError(window, @"未能找到打开此文件的应用程序");
            }
        }
    }
}